#include <iostream>
#include "avalon/netbus.h"
#include <thread>
#include "bar_provider.h"
#include "foo_provider.h"
#include "avalon/deliver.h"

int main(int argc, char *argv[])
{
    // 初始化netbus并设置配置信息
    NetBus *netbus = NetBus::GetInstance();
    netbus->setHost("127.0.0.1");
    netbus->setPort(5672);
    netbus->setUsername("guest");
    netbus->setPassword("guest");
    netbus->setThreadNum(2);
    // 启动netbus
    netbus->start();

    
    BarProvider bar_provider;
    FooProvider foo_provider;
    
    // 注册provider
    netbus->addProvider(&bar_provider);
    netbus->addProvider(&foo_provider);

    std::string r = "hello world";

    // 发送消息
    Deliver delos("timer", "message", r.c_str(), r.size(), FLAG::OnlySend);
    if(netbus->send(&delos))
    {
        std::cout << "send success" << std::endl;
    }

    // 发送并接收消息
    Deliver delsr("timer", "ping", r.c_str(), r.size(), FLAG::SendAndReceived);
    if(netbus->sendAndReceived(&delsr))
    {
        std::string res(delsr.reply()->data, delsr.reply()->size);
        std::cout << "received: " << res << std::endl;
    }

    std::this_thread::sleep_for(std::chrono::seconds(1000));
    return 0;
}